function is_weixin() {
  var ua = navigator.userAgent.toLowerCase();
  if ((ua.match(/MicroMessenger/i) == 'micromessenger') && (ua.match(/wxwork/i) == 'wxwork')) {
    return false;
  } else if (ua.match(/micromessenger/i) == 'micromessenger') {
    return true;
  } else {
    return false;
  }
}

async function common_get(path) {
  return new Promise(function (resolve, reject) {
    axios.get(getApiHost() + 'common_api/' + path).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}

async function common_post(path, data) {
  return new Promise(function (resolve, reject) {
    axios.post(getApiHost() + 'common_api/' + path, data).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}

async function employee_get(path) {
  return new Promise(function (resolve, reject) {
    axios.get(getApiHost() + 'employee_api/' + path, {
      headers: {
        Authorization: 'Bearer ' + localStorage.getItem('employee_token'),
      }
    }).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      if (error.response) {
        if (error.response.status === 401) {
          localStorage.removeItem('employee_token')
          window.location.href = 'index.html';
          return;
        }
      }
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}

async function employee_post(path, data) {
  return new Promise(function (resolve, reject) {
    axios.post(getApiHost() + 'employee_api/' + path, data, {
      headers: {
        Authorization: 'Bearer ' + localStorage.getItem('employee_token'),
      }
    }).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      if (error.response) {
        if (error.response.status === 401) {
          localStorage.removeItem('employee_token')
          window.location.href = 'index.html';
          return;
        }
      }
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}


async function merchant_get(path) {
  return new Promise(function (resolve, reject) {
    axios.get(getApiHost() + 'merchant_api/' + path, {
      headers: {
        Authorization: 'Bearer ' + localStorage.getItem('merchant_token'),
      }
    }).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      if (error.response) {
        if (error.response.status === 401) {
          localStorage.removeItem('merchant_token')
          window.location.href = 'index.html';
          return;
        }
      }
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}

async function merchant_post(path, data) {
  return new Promise(function (resolve, reject) {
    axios.post(getApiHost() + 'merchant_api/' + path, data, {
      headers: {
        Authorization: 'Bearer ' + localStorage.getItem('merchant_token'),
      }
    }).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      if (error.response) {
        if (error.response.status === 401) {
          localStorage.removeItem('merchant_token')
          window.location.href = 'index.html';
          return;
        }
      }
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}

async function client_get(path) {
  return new Promise(function (resolve, reject) {
    axios.get(getApiHost() + 'api/' + path, {
      headers: {
        Authorization: 'Bearer ' + getCookie('access_token'),
      }
    }).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      if (error.response) {
        if (error.response.status === 401) {
          var sourceUrl = window.location.href;
          localStorage.setItem("login_source", JSON.stringify(sourceUrl));
          window.location.href = '/';
          return;
        }
      }
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}

async function client_post(path, data) {
  return new Promise(function (resolve, reject) {
    axios.post(getApiHost() + 'api/' + path, data, {
      headers: {
        Authorization: 'Bearer ' + getCookie('access_token'),
      }
    }).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      if (error.response) {
        if (error.response.status === 401) {
          var sourceUrl = window.location.href;
          localStorage.setItem("login_source", JSON.stringify(sourceUrl));
          window.location.href = '/';
          return;
        }
      }
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}

async function talent_get(path) {
  return new Promise(function (resolve, reject) {
    axios.get(getApiHost() + 'talent_api/' + path, {
      headers: {
        Authorization: 'Bearer ' + getCookie('talent_token'),
      }
    }).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      if (error.response) {
        if (error.response.status === 401) {
          var sourceUrl = window.location.href;
          localStorage.setItem("login_source", JSON.stringify(sourceUrl));
          window.location.href = '/talent_reg';
          return;
        }
      }
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}

async function talent_post(path, data) {
  return new Promise(function (resolve, reject) {
    axios.post(getApiHost() + 'talent_api/' + path, data, {
      headers: {
        Authorization: 'Bearer ' + getCookie('talent_token'),
      }
    }).then(res => {
      if (res.status !== 200) {
        layer.msg(res.data.msg)
      }
      if (res.data.code === 'ok') {
        resolve(res.data.data)
      } else {
        layer.msg(res.data.msg)
      }
    }).catch(function (error) {
      if (error.response) {
        if (error.response.status === 401) {
          var sourceUrl = window.location.href;
          localStorage.setItem("login_source", JSON.stringify(sourceUrl));
          window.location.href = '/talent_reg';
          return;
        }
      }
      layer.msg('服务器开小差了，请稍后再试')
    });
  })
}

function getApiHost() {
  let url = document.location.toString()
    , arrUrl = url.split("//")
    , scheme = arrUrl[0]
    , start = arrUrl[1].indexOf("/")
    , domain = arrUrl[1].substring(0, start + 1)

  return scheme + '//' + domain;
}

function getCookie(name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  if (arr = document.cookie.match(reg)) {
    return unescape(arr[2]);
  }
  return null;
}

function setCookie(name, value, seconds) {
  var exp = new Date();
  exp.setTime(exp.getTime() + seconds * 1000);
  document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getUrlParam(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}